package no.hib.mod250.chat.client.view;

import no.hib.mod250.chat.client.controller.ChatController;
import no.hib.mod250.chat.client.i18.ChatConstants;
import no.hib.mod250.chat.client.util.ObjectWithEvent;
import no.hib.mod250.chat.client.util.R;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MessageView extends View {

	// CLASSES

	private class MessageTextArea extends RichTextArea implements KeyUpHandler {
		public MessageTextArea() {
			addKeyUpHandler(this);
		}

		@Override
		public void onKeyUp(KeyUpEvent event) {
			if (event.isControlKeyDown()
					&& event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
				doChat();
			}
		}
	}

	// /CLASSES

	// VARIABLES

	// ... VIEW

	private HorizontalPanel panel = new HorizontalPanel();
	private VerticalPanel textAreaPanel = new VerticalPanel();
	private Button btnChat = new Button();
	private MessageTextArea textArea = new MessageTextArea();
	private RichTextToolbar richTextToolbar;

	// ... /VIEW

	private static ChatConstants.Message messageConstants = GWT
			.create(ChatConstants.Message.class);

	// /VARIABLES

	// CONSTRUCTOR

	public MessageView(ChatController controller) {
		super(controller);

		// Set composite to wrap panel
		initWidget(panel);

		// Draw view and add events
		doDraw();
		doEvents();
	}

	// /CONSTRUCTOR

	// FUNCTIONS

	@Override
	protected void doDraw() {
		// Panel
		panel.setStyleName(R.Class.Message.message);

		// Chat text area
		textArea.setEnabled(false);
		textArea.setSize("100%", "100%");

		// Chat text area toolbar
		richTextToolbar = new RichTextToolbar(textArea,
				RichTextToolbar.TOOLBOX_BIUS);

		textAreaPanel.add(richTextToolbar);
		textAreaPanel.setCellWidth(richTextToolbar, "100%");
		textAreaPanel.add(textArea);
		textAreaPanel.setCellHeight(textArea, "100%");
		textAreaPanel.setCellWidth(textArea, "80%");
		
		panel.add(textAreaPanel);
		panel.setCellHeight(textAreaPanel, "100%");
		panel.setCellWidth(textAreaPanel, "100%");
		
		textAreaPanel.setSize("100%", "100%");
		
		// Chat button
		panel.add(btnChat);
		btnChat.setText(messageConstants.chat());
		btnChat.setStyleName(R.Class.Message.chatButton);
		btnChat.setEnabled(false);
		panel.setCellVerticalAlignment(btnChat,
				HasVerticalAlignment.ALIGN_MIDDLE);
		panel.setCellHorizontalAlignment(btnChat,
				HasHorizontalAlignment.ALIGN_CENTER);
		panel.setCellHeight(btnChat, "100%");
	}

	protected void doEvents() {
		btnChat.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				doChat();
			}
		});

		// Add listener to "Chat enabled" variable
		getController().getIsChatEnabled().addListener(
				new ObjectWithEvent.Listener<Boolean>() {

					@Override
					public void onChanged(Boolean value) {
						doUpdate();
					}

				});
	}

	/**
	 * Create new chat message
	 */
	private void doChat() {
		// Chat must be enabled
		if (!getController().getIsChatEnabled().getObject()) {
			return;
		}

		// Get message
		String msg = textArea.getHTML();

		// Don't accept empty messages
		if (msg.trim().equalsIgnoreCase("")) {
			return;
		}

		// Empty text area
		textArea.setText("");

		// Focus on text area
		textArea.setFocus(true);

		// New Message
		getController().doMessageNew(msg);
	}

	/**
	 * Enables and disabled text area and chat button
	 */
	private void doUpdate() {
		if (!getController().getIsChatEnabled().getObject()) {
			textArea.setEnabled(false);
			btnChat.setEnabled(false);
		} else {
			textArea.setEnabled(true);
			btnChat.setEnabled(true);
		}
	}

	// /FUNCTIONS

}
